import { createRouter, createWebHistory } from 'vue-router';

// 使用动态导入方式加载页面配置和组件
const pages = import.meta.glob('@/pages/**/page.js', {
    eager: true,
    import: 'default'
});
const componentPath = import.meta.glob('@/pages/**/index.vue');

// 生成基础路由
const routes = Object.entries(pages).map(([path, meta]) => {
    const component = componentPath[path.replace('page.js', 'index.vue')];
    const routePath = path.replace('/src/pages', '').replace('/page.js', '') || '/';
    return {
        path: routePath,
        name: routePath.replace('/', '') || 'home',
        component,
        meta: meta
    };
});

// 静态路由配置
const staticRoutes = [
    {
        path: '/',
        redirect: '/home'
    },
    // {
    //     path: '/:pathMatch(.*)*',
    //     name: 'not-found',
    //     component: () => import('@/pages/notFound/index.vue')
    // }
];

const router = createRouter({
    history: createWebHistory(),
    routes: [...staticRoutes, ...routes], // 将静态路由放在前面
    scrollBehavior(to, from, savedPosition) {
        return savedPosition || { top: 0 };
    }
});

export default router;